var express = require('express');
var router = express.Router();
const dao = require('../dao/index.js');
const Cache = require('../utils/cache')
const permissionCheck = require('../midware/permission')

/* 题目列表. */
router.get('/', async function(req, res, next) {
	let pageSize = req.query.pageSize ?? 10;
	let pageNum = req.query.pageNum ?? 1;
	
	
	let sql1 = `
		select s.id, s.title, s.type, s.category, u.name as author, s.last_modify from t_subject s 
		left join t_user u on (s.authorId = u.id) where s.deleted = 0 
	`
	let sql2 = `select count(s.id) as total from t_subject s left join t_user u on (s.authorId = u.id) where s.deleted = 0 `
	
	let type = req.query.type ?? undefined
	let category = req.query.category ?? undefined
	let title = req.query.title ?? undefined
	if(title) {
		sql1 += `and s.title like '%${title}%' `
		sql2 += `and s.title like '%${title}%' `
	}
	if(type) {
		sql1 += `and s.type = ${type} `
		sql2 += `and s.type = ${type} `
	}
	if(category>0) {
		sql1 += `and s.category = ${category} `
		sql2 += `and s.category = ${category} `
	}
	
	
	sql1 += `order by s.last_modify desc limit ${pageSize*(pageNum-1)}, ${pageSize}`
	
	try {
		let result1 = await dao.query(sql1)
		let result2 = await dao.query(sql2)
		
		res.send({
			code: 0,
			msg: '查询成功',
			result: result1,
			total: result2[0].total
		})
	} catch(e) {
		res.send(e.toString())
	}
});

/* 题目详情 */
router.get('/detail', function(req, res, next) {
	let id = req.query.id;
	if(!id) {
		res.send({ code: 100001, msg: '缺少查询条件【ID】' })
		return;
	}
	
	let sql = `
		select s.id, s.title, s.content, s.type, s.category, u.name as author, s.last_modify from t_subject s 
		left join t_user u on (s.authorId = u.id) where s.id=${id}
	`
	dao.query(sql).then(result=>{
		res.send({
			'code': 0,
			'msg': '查询成功',
			'result': result[0]
		})
	}).catch(e=>{
		res.send(e.toString())
	})
});


/* 保存题目 */
router.post('/create', permissionCheck,  function(req, res, next) {
	let token = req.header('x-token')
	let loginuser = Cache.get(token)
	if(loginuser.is_admin == 0) {
		res.send({
			'code': 100004,
			'msg': '非管理员无权执行该操作'
		})
		return;
	}
	
	let body = req.body;
	let sql = `INSERT INTO t_subject VALUES (null, ?, ?, ? , ?, ?, ?, 0)`
	let insertArr = [body.title, body.content, loginuser.id, body.type, body.category, new Date().format('yyyy-MM-dd hh:mm:ss')]
	dao.query(sql, insertArr).then(result=>{
		res.send({
			'code': 0,
			'msg': '插入成功',
			'result': result
		})
	}).catch(e=>{
		res.send(e.toString())
	})
});

/* 修改题目 */
router.post('/modify', permissionCheck,  function(req, res, next) {
	let token = req.header('x-token')
	let body = req.body;
	let loginuser = Cache.get(token)
	
	if(loginuser.is_admin == 0) {
		res.send({
			'code': 100004,
			'msg': '非管理员无权执行该操作'
		})
		return;
	}
	let sql = `UPDATE t_subject SET title=?, content=?, authorId=?, type=?, category=?, last_modify=? WHERE id=?`;
	let updateArr = [body.title, body.content, loginuser.id, body.type, body.category, new Date().format('yyyy-MM-dd hh:mm:ss'), body.id]
	dao.query(sql, updateArr).then(result=>{
		res.send({
			'code': 0,
			'msg': '修改成功'
		})
	}).catch(e=>{
		res.send(e.toString())
	})
});

/* 删除题目 */
router.post('/remove', permissionCheck, function(req, res, next) {
	let token = req.header('x-token')
	let body = req.body;
	let loginuser = Cache.get(token)
	
	if(loginuser.is_admin == 0) {
		res.send({
			'code': 100004,
			'msg': '非管理员无权执行该操作'
		})
		return;
	}
	
	let sql = `UPDATE t_subject SET deleted=1 WHERE id=${body.id}`
	dao.query(sql).then(result=>{
		res.send({
			'code': 0,
			'msg': '修改成功'
		})
	}).catch(e=>{
		res.send(e.toString())
	})
});

module.exports = router;
